package com.kgc.dao;

import com.kgc.entity.Danyuan;
import com.kgc.untils.PageUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDanYuanDao extends BaseDao{


    /**
     * 查询有房屋分配单元列表
     * @return
     */
    public  List<Danyuan> selectDanyuanRoom() {
        List<Danyuan> list = new ArrayList<>();
        try {
            String sql = "select * from danyuan where id in ( select DISTINCT danyuan_id from room  where status = 0)";
            ResultSet rs = super.getExecuteQuery(sql);
            while (rs.next()) {
                list.add(new Danyuan(rs.getInt(1), rs.getString(2), rs.getInt(3)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return list;

    }

    //查找全部的单元数据
    public List<Danyuan> selectDanYuan(PageUtils pageUtils , String name){
        List<Danyuan> list = new ArrayList<>();
        try {
            String sql = "SELECT d.id,d.`name`,b.`name`,d.building_id FROM danyuan d INNER JOIN building b ON d.building_id=b.id ";
            if(StringUtils.isNotEmpty(name)){
                sql += " where d.name like '%"+name+"%'";
            }
            sql+=" limit ?,?";
            ResultSet rs = super.getExecuteQuery(sql, pageUtils.start(), pageUtils.getPageSize());
            while (rs.next()){
                list.add(new Danyuan(rs.getInt(1),rs.getString(2),rs.getInt(4),rs.getString(3)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return list;
    }
//查找总单元数
    public int totalDanYuan(String name){
        try {
            String sql = "select count(*) from danyuan d INNER JOIN building b ON d.building_id=b.id";
            if (StringUtils.isNotEmpty(name)) {
                sql += " where d.`name` like '%" + name + "%'";
            }
            ResultSet rs = super.getExecuteQuery(sql);
            while (rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
//添加单元
    public int addDanyuan(Danyuan danyuan){
        return super.getExecuteUpdate("INSERT INTO `danyuan` VALUES (null, ?, ?);"
                ,danyuan.getName(),danyuan.getBuildingId());
    }
//删除单元
    public int deleteBuilding(String id) {
        return super.getExecuteUpdate("delete from danyuan where id=?",id);
    }

    public int updatedanyuan(Danyuan danyuan) {
        String sql = "UPDATE `danyuan` SET `name` = ?, `building_id` = ? WHERE `id` = ?";
        return super.getExecuteUpdate(sql, danyuan.getName(), danyuan.getBuildingName(), danyuan.getId());
    }
    public List<Danyuan> selectdanyuan(){
        List<Danyuan> list = new ArrayList<>();
        try {
            String sql = "select * from danyuan ";
            ResultSet rs = super.getExecuteQuery(sql);
            while (rs.next()){
                list.add(new Danyuan(rs.getInt(1),rs.getString(2),rs.getInt(3)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return list;
    }
}
